package com.firegod.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.firegod.commonutils.result.ResultCode;
import com.firegod.commonutils.vo.ResultVo;
import com.firegod.servicebase.exceptionhandler.exception.GuliException;
import com.firegod.student.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "学生管理")
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/login")
    @ApiOperation("学生登录")
    public ResultVo login(@RequestBody JSONObject jsonObject) {
        String studentId = jsonObject.getString("userId");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(studentId)) {
            throw GuliException.from(ResultCode.NO_STUDENT_ID_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        String token = studentService.login(studentId, password);
        return ResultVo.ok().data("token", token);
    }

    @PostMapping("/register")
    @ApiOperation("学生注册")
    public ResultVo register(@RequestBody JSONObject jsonObject) {
        String studentId = jsonObject.getString("userId");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(studentId)) {
            throw GuliException.from(ResultCode.NO_STUDENT_ID_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        String token = studentService.register(studentId, password);
        return ResultVo.ok().data("token", token);
    }

    @PostMapping("/changePassword")
    @ApiOperation("学生修改密码")
    public ResultVo changePassword(@RequestBody JSONObject jsonObject) {
        String oldPassword = jsonObject.getString("oldPassword");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(oldPassword)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        studentService.changePassword(oldPassword, password);
        return ResultVo.ok();
    }

    @GetMapping("/studentInfo")
    @ApiOperation("学生信息")
    public ResultVo studentInfo() {
        return ResultVo.ok().data(studentService.getStudentInfo());
    }

//    @GetMapping("/studentInfo")
//    @ApiOperation("学生信息")
//    public ResultVo studentInfo() {
//        return ResultVo.ok().data(studentService.getStudentInfo());
//    }

    @PostMapping("/changeUserInfo")
    @ApiOperation("修改学生信息")
    public ResultVo changeStudentInfo(@RequestBody JSONObject jsonObject) {
        String mobile = jsonObject.getString("mobile");
        String email = jsonObject.getString("email");
        String name = jsonObject.getString("name");
        studentService.changeInfo(mobile, email, name);
        return ResultVo.ok();
    }

    @GetMapping("/myCourse")
    @ApiOperation("学生选课信息")
    public ResultVo getCourses() {
        return ResultVo.ok().data("courseList", studentService.getCourse());
    }
}
